package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Comoconocido;
import com.liwalo.entities.Pais;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Pais.
 * 
 * @see com.liwalo.dao.Pais
 * @author Hibernate Tools
 */
public class PaisDAO {

	private static final Log log = LogFactory.getLog(PaisDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil
			.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(Pais transientInstance) {
		log.debug("persisting Pais instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Pais instance) {
		log.debug("attaching dirty Pais instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Pais instance) {
		log.debug("attaching clean Pais instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Pais persistentInstance) {
		log.debug("deleting Pais instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Pais merge(Pais detachedInstance) {
		log.debug("merging Pais instance");
		try {
			Pais result = (Pais) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Pais findById(java.lang.Integer id) {
		log.debug("getting Pais instance with id: " + id);
		try {
			Pais instance = (Pais) sessionFactory.getCurrentSession().get(
					"com.liwalo.dao.Pais", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Pais> findByExample(Pais instance) {
		log.debug("finding Pais instance by example");
		try {
			List<Pais> results = (List<Pais>) sessionFactory
					.getCurrentSession().createCriteria("com.liwalo.dao.Pais")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Pais> getAllValues() {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Pais.class)
								   .add(Restrictions.eq("estado",1));
		return (List<Pais>) criteria.list();
	}
}
